<?php

/*
 * This file is part of the Geek-Zoo Projects.
 *
 * @copyright (c) 2010 Geek-Zoo Projects More info http://www.geek-zoo.com
 * @license http://opensource.org/licenses/gpl-2.0.php The GNU General Public License
 * @author xuanyan <xuanyan@geek-zoo.com>
 *
 */

require_once ROOT_PATH.'/Abstract/front_abstract.php';

class Action extends front_abstract
{
    function index()
    {
        $api = new ApiSina();
        $return = $api->getAuthorizeURL(SITE_URL.'/account/bind.html');
        $_SESSION['bind_tmp'] = $return;
        $this->smarty->display('account.html');
    }

    function bind()
    {
            $key = $this->api->callback($_SESSION['bind_tmp']['oauth_token'], $_SESSION['bind_tmp']['oauth_token_secret']);
            if (isset($key['error_CN'])) {
                $this->redirect("account.html?msg=".urlencode('token过期请重新添加!'));
            }

            if ($_SESSION['user']['parent_id'] != 0) {
                $this->redirect("index.html?msg=".urlencode('您没有添加权限!'));
            }

            // 判断是否绑定过
            if(!$info = _model('account')->read(array('id'=>$key['user_id'])) ){
                _model('account')->create(array(
                    'id' => $key['user_id'],
                    'oauth_token' => $key['oauth_token'],
                    'oauth_token_secret' => $key['oauth_token_secret'],
                    'is_bind' => 1
                ));
            }else{
                $a = _model('account')->update(array('id'=>$key['user_id']), array('oauth_token' => $key['oauth_token'], 'oauth_token_secret' => $key['oauth_token_secret']));
            }

            if ($info = _model('user')->read(array('account_id'=>$key['user_id']))) { // 有记录,不是自己可以管理的,提示错误
                if ($info['parent_id'] != $_SESSION['user']['id']) {
                    $this->redirect("account.html?msg=".urlencode('此用户已经被添加, 您没有权限添加'));
                }
            } else { // 无记录,插入
                $info['user_name'] = $key['user_id'];
                $info['account_id'] = $key['user_id'];
                $info['parent_id'] = $_SESSION['user']['id'];
                $info['add_time'] = time();
                if (!_model('user')->create($info)) {
                    $this->redirect("account.html?msg=".urlencode('添加失败请重新绑定!'));
                }
                
                if( $info = _model('user')->read(array('id'=>$_SESSION['user']['id'])) ){
                    $accounts = array_filter(explode(',', $info['accounts']));
                    $accounts[] = $key['user_id'];
                    $acc = implode(',' , $accounts);
                    if( !$a = _model('user')->update(array('id' => $_SESSION['user']['id']), array('accounts' => $acc)) ){
                        $this->redirect("account.html?msg=".urlencode('插入数据错误'));
                    }
                }
            }
            
            _model('account')->update(array('id' => $key['user_id']), array('is_bind'=>1));


            $this->redirect('account.html');
    }
    
    function delete(){
        if ($info = _model('user')->read(array('account_id'=>_GET('account_id', 0)))) {
            if ($info['parent_id'] == $_SESSION['user']['id']) {
                _model('account')->update(array('id'=>$info['account_id']), array('is_bind'=>0));
            }
        }
        $this->redirect('account.html');
            
        //if( !_model('account')->update(array()) )
    }
}
?>
